package vj.string;

public class DetectAnagram {
	
	//Assign the primes P_1 to P_26 to the letters
	// 
	private static int prime[] = new int[26];
	
	static {
	prime[0] = 7;
	prime[1] = 11;
	prime[2] = 13;
	prime[3] = 17;
	prime[4] = 23;

	}
	public static void main(String[] args) {
		System.out.println(isAnagram("abacd","dcaba"));
		System.out.println(isAnagram("bacd","dcab"));
		System.out.println(isAnagram("babbcbd","dbcabbb"));
		System.out.println(isAnagram("bacd","dcbb"));
		System.out.println(isAnagram("babbacbad","dbacabbab"));
		System.out.println(isAnagram("babbcccbd","dbcacbbb"));
	}
	
	private static boolean isAnagram(String a, String b) {
		double prodA = 1, prodB = 1;
		for(int i = 0; i < a.length(); i++) 
			prodA = prodA * prime[a.charAt(i) - 'a'];
		
		for(int i = 0; i < b.length(); i++) 
			prodB = prodB * prime[b.charAt(i) - 'a'];
		
		double res = prodA/prodB;
		if(res == 1.0)
			return true;
		return false;
	}
}
